/*
单链表
双链表
环链表

修改（增删改）时间复杂度 O(1)
查询时间复杂度O(n)
 */

package main

import "fmt"

type Node struct {
	Data int
	Next *Node
	Pre *Node
}

type LinkedList struct {
	head *Node
	curr *Node
	tail *Node
}

//创建一个链表
func CreateLinkedList(){
	data := []int{1, 21, 31, 51, 62, 2, 3, 42, 33, 12, 12}
	link := LinkedList{}
	var currentNode *Node
	for i := 0; i < len(data); i++ {
		currentNode = new(Node)
		currentNode.Data = data[i]
		insertNode(&link, currentNode)
	}
	showLinkedList(link)
}

//插入节点
func insertNode(link *LinkedList, node *Node){
	if link.head == nil {
		link.head = node
		link.curr = node
		link.tail = node
		return
	}
	link.tail.Next = node
	node.Pre = link.tail
	link.tail = node

}

func showLinkedList(link LinkedList) {
	curr := link.head
	for {
		if curr != nil {
			fmt.Printf("%v ",curr.Data)
			curr = curr.Next
		}else {
			break
		}
	}
}

func main() {
	CreateLinkedList()
}